using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using LinkPointTransaction;
//using LPICOM_6_0Lib;
using System.Collections.Specialized;
using System.Configuration;
using SiteManager;
using System.Text;

    public partial class Doctor_subscription : System.Web.UI.Page
    {

        Manager ObjManager = new Manager(true,"ConnectionString");

        protected string configfile;
        protected string keyfile;
        protected string host;
        protected int port;
        protected LinkPointTxn LPTxn;
        protected bool fLog = false;
        protected int logLvl = 0;
        protected string logFile = "";
        protected string baddrnum = "";
        protected string bzip = "";
        protected string Name = "";
        protected string Email = "";
        protected string cardnumber = "";
        protected string expmonth = "03";
        protected string expyear = "05";
        protected int expyearVal = 2005;
        protected string total = "";
        protected string startdate = System.DateTime.Now.ToString();
        protected string periodicity = "";
        protected string installments = "";
        protected string pbthreshold = "";



        protected void Page_Load(object sender, EventArgs e)
        {

            localFunction drchk = new localFunction();
            drchk.checkDoctorLogin(true);
            if ( !Page.IsPostBack)
            {
                LoadCommonControls();
            }
            if (IsPostBack)
            {
                if (WizardSubscription.ActiveStepIndex == 1)
                {
                    //Parse form data
                    ParseFormData();
                    // process order
                    ProcessOrder();



                    //Status Page
                    // Put user code to initialize the page here

                    HttpBrowserCapabilities bc = Request.Browser;
                    fIE5 = (bc.Browser == "IE" && bc.MajorVersion > 4);

                    order = (string)Session["outXml"];
                    resp = (string)Session["resp"];
                    ParseResponse(resp);
                    // We need LPTxn to display version info
                    LPTxn = new LinkPointTxn();
                    //
                }

            }

          
        }

     
       
        private void ProcessOrder()
        {
            #region -- Akash Code --

            // create order            
            LPOrderPart order = LPOrderFactory.createOrderPart("order");
            // create a part we will use to build the order
            LPOrderPart op = LPOrderFactory.createOrderPart();

            // Build 'orderoptions'
            op.put("ordertype", "SALE");
            // add 'orderoptions to order
            order.addPart("orderoptions", op);

            // Build 'merchantinfo'
            op.clear();
            op.put("configfile", configfile);
            // add 'merchantinfo to order
            order.addPart("merchantinfo", op);

            // Build 'billing'
            // Required for AVS. If not provided, 
            // transactions will downgrade.
            op.clear();
            op.put("zip", bzip);
            op.put("addrnum", baddrnum);
            // add 'billing to order
            order.addPart("billing", op);

            // Build 'creditcard'
            op.clear();
            op.put("cardnumber", cardnumber);
            op.put("cardexpmonth", expmonth);
            op.put("cardexpyear", expyear);
            // add 'creditcard to order
            order.addPart("creditcard", op);

            // Build 'payment'
            op.clear();
            op.put("chargetotal", total);
            // add 'payment to order
            order.addPart("payment", op);

            // build 'periodic' part
            op.clear();
            op.put("action", "SUBMIT");
            op.put("startdate", startdate);
            op.put("periodicity", periodicity);
            op.put("installments", installments);
            op.put("threshold", pbthreshold);
            // add 'periodic' to order
            order.addPart("periodic", op);

            // create transaction object	
            LPTxn = new LinkPointTxn();

            // get outgoing XML from the 'order' object
            string outXml = order.toXML();

            // Call LPTxn
            string resp = LPTxn.send(keyfile, host, port, outXml);

            //Store transaction data on Session and redirect
            Session["outXml"] = outXml;
            Session["resp"] = resp;

            #endregion

            //#region -- Kajal Code --

                       
            //// create order   
            //LPOrderPart order = new LPOrderPart();
            //order.setPartName("order");

            //// create a part we will use to build the order
            //LPOrderPart op = new LPOrderPart();
            //// Build 'orderoptions'
            //op.put("ordertype", "SALE");
            //// add 'orderoptions to order
            //order.addPart("orderoptions", op, 0);

            //// Build 'merchantinfo'
            //op.clear();
            //op.put("configfile", configfile);
            //// add 'merchantinfo to order
            //order.addPart("merchantinfo", op, 1);

            //// Build 'billing'
            //// Required for AVS. If not provided, 
            //// transactions will downgrade.
            //op.clear();
            //op.put("zip", bzip);
            //op.put("addrnum", baddrnum);
            //// add 'billing to order
            //order.addPart("billing", op, 3);

            //// Build 'creditcard'
            //op.clear();
            //op.put("cardnumber", cardnumber);
            //op.put("cardexpmonth", expmonth);
            //op.put("cardexpyear", expyear);
            //// add 'creditcard to order
            //order.addPart("creditcard", op, 4);

            //// Build 'payment'
            //op.clear();
            //op.put("chargetotal", total);
            //// add 'payment to order
            //order.addPart("payment", op, 5);

            //// build 'periodic' part
            //op.clear();
            //op.put("action", "SUBMIT");
            //op.put("startdate", startdate);
            //op.put("periodicity", periodicity);
            //op.put("installments", installments);
            //op.put("threshold", pbthreshold);
            //// add 'periodic' to order
            //order.addPart("periodic", op, 6);

            //// create transaction object	

            //LPTxn = new LinkPointTxn();

            //// get outgoing XML from the 'order' object
            //object outXml = order.toXML();

            //// Call LPTxn
            //object resp = LPTxn.send(keyfile, host, port, outXml);

            ////Store transaction data on Session and redirect
            //Session["outXml"] = outXml;
            //Session["resp"] = resp;

            //#endregion


        }
        public void WriteYearOpts()
        {
            int year = DateTime.Now.Year;
            string pad = "";
            string sel = "";

            try
            {
                expyearVal = Int32.Parse(expyear);
                if (expyearVal < 10) expyearVal += 2000;
            }
            catch (Exception)
            {
                expyearVal = year;
            }

            for (int yr = year; yr <= year + 5; yr++)
            {
                sel = expyearVal == yr ? " selected " : "";
                pad = (yr - 2000) < 10 ? "0" : "";
                Response.Write("<option value=" + pad + (yr - 2000) + sel + ">" + yr + "</option>");
            }
        }

        public void WriteMonthOpts()
        {
            int mn;
            int cur = Int32.Parse(expmonth);
            string pad = "";


            for (mn = 1; mn <= 12; mn++)
            {
                pad = mn < 10 ? "0" : "";
                if (mn == cur)
                    Response.Write("<option value=" + pad + mn + " selected>" + mn + "</option>");
                else
                    Response.Write("<option value=" + pad + mn + ">" + mn + "</option>");
            }

        }
        protected void ParseFormData()
        {
            bzip = txtbzip.Text;
            baddrnum = txtbaddrnum.Text;
            cardnumber = txtcardnumber.Text;
            expmonth = Request.Form["expmonth"];
            expyear = Request.Form["expyear"];
            total = txttotal.Text;
            Name = txtName.Text;
            Email=txtEmail.Text;
            if ((total == null) || total.Length < 1) { total = "0.0"; }
            pbthreshold = txtpbthreshold.Text;
            startdate = System.DateTime.Now.ToString();
            periodicity = ddlSubscriptionType.Text;
            installments = txtinstallments.Text;
            GetConfigParams();
        }
        private void GetConfigParams()
        {
            // get some stuff from app config
            //NameValueCollection config1 = (NameValueCollection)ConfigurationSettings.GetConfig("LPAPP_Config/client"); commented at 131008 to fix
            NameValueCollection config1 = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("LPAPP_Config/client");


            configfile = config1["Configfile"];
            keyfile = config1["Keyfile"];
            host = config1["Host"];
            port = Int32.Parse(config1["Port"]);

            //NameValueCollection config = (NameValueCollection)ConfigurationSettings.GetConfig("LPAPP_Config/opts"); commented at 131008 to fix
            NameValueCollection config = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("LPAPP_Config/opts");


            try
            {
                fLog = bool.Parse(config["fLog"]);
            }
            catch (Exception) { }

            try
            {
                logLvl = Int32.Parse(config["logLvl"]);
            }
            catch (Exception) { }

            try
            {
                logFile = config["logFile"];
            }
            catch (Exception) { }

        }



        protected void Submit_Click(object sender, EventArgs e)
        {
            ////Response.Redirect("status.aspx");
            //WizardSubscription.MoveTo(WizardStep3);
        }
        private void LoadCommonControls()
        {
            ObjManager.BindList("select id,name from tbl_speciality order by name", "name", "id", ddlSpeciality, "");
            ObjManager.BindList("select FeatureDocTypeId,FeatureDocType from  tbl_FeatureDocType", "FeatureDocType", "FeatureDocTypeId", ddlFeaturedDocType, "");

            ddlSubscriptionType.Items.Add(new ListItem("Monthly", "1"));
            ddlSubscriptionType.Items.Add(new ListItem("Annually", "2"));


        }
        //Status Page
        protected void ParseResponse(string rsp)
        {
            R_Time = ParseTag("r_time", rsp);
            R_Ref = ParseTag("r_ref", rsp);
            R_Approved = ParseTag("r_approved", rsp);
            R_Code = ParseTag("r_code", rsp);
            R_Authresr = ParseTag("r_authresronse", rsp);
            R_Error = ParseTag("r_error", rsp);
            R_OrderNum = ParseTag("r_ordernum", rsp);
            R_Message = ParseTag("r_message", rsp);
            R_Score = ParseTag("r_score", rsp);
            R_TDate = ParseTag("r_tdate", rsp);
            R_AVS = ParseTag("r_avs", rsp);
            R_Tax = ParseTag("r_tax", rsp);
            R_Shipping = ParseTag("r_shipping", rsp);
            R_FraudCode = ParseTag("r_fraudCode", rsp);
            R_ESD = ParseTag("esd", rsp);
        }
                
 
        
        protected string ParseTag(string tag, string rsp)
        {
            StringBuilder sb = new StringBuilder(256);
            sb.AppendFormat("<{0}>", tag);
            int len = sb.Length;
            int idxSt = -1, idxEnd = -1;
            if (-1 == (idxSt = rsp.IndexOf(sb.ToString())))
            { return ""; }
            idxSt += len;
            sb.Remove(0, len);
            sb.AppendFormat("</{0}>", tag);
            if (-1 == (idxEnd = rsp.IndexOf(sb.ToString(), idxSt)))
            { return ""; }
            return rsp.Substring(idxSt, idxEnd - idxSt);
        }
        
        protected string order;
        protected string resp;
        protected bool fIE5;
        protected string R_Time;
        protected string R_Ref;
        protected string R_Approved;
        protected string R_Code;
        protected string R_Authresr;
        protected string R_Error;
        protected string R_OrderNum;
        protected string R_Message;
        protected string R_Score;
        protected string R_TDate;
        protected string R_AVS;
        protected string R_FraudCode;
        protected string R_ESD;
        protected string R_Tax;
        protected string R_Shipping;

        //
        protected void WizardSubscription_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Response.Write("Button Click");

        }
}

